<template>
  <div class="app-container">

    <el-row style="padding-bottom: 5px;">
      <el-col :span="24">
        <!--查询条件-->
        <el-form :inline="true" :model="listQuery" label-width="130px">
          <el-form-item label="分类名称:" prop='name'>
            <el-input v-model="listQuery.keyWord"  ></el-input>
          </el-form-item>
          <el-form-item label="是否关联属性集合:"   prop='mappingAttrCategory'>
            <el-select style="width: 150px;" clearable  v-model="listQuery.isMappingAttrCategory"  placeholder="请选择" >
              <el-option
                v-for="item in mappingAttrCategoryTypeItemList"
                :key="item.value"
                :label="item.name"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <div style="display:inline-block;width: 100px;"></div>
            <el-button type="primary" @click="search">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">数据列表</span>
      <el-button
        class="btn-add"
        @click="handleAddProductCate()"
        size="mini">
        添加
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="productCateTable"
                style="width: 100%"
                :data="list"
                v-loading="listLoading" border>
        <el-table-column label="编号" width="50" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="分类名称" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="级别" width="50" align="center">
          <template slot-scope="scope">{{scope.row.level | levelFilter}}</template>
        </el-table-column>
        <el-table-column label="商品数量" width="70" align="center">
          <template slot-scope="scope">{{scope.row.productCount }}</template>
        </el-table-column>
        <el-table-column label="数量单位" width="70" align="center">
          <template slot-scope="scope">{{scope.row.productUnit }}</template>
        </el-table-column>
        <el-table-column label="关联属性集合" width="80" align="center">
          <template slot-scope="scope">{{scope.row.attributeSetNames }}</template>
        </el-table-column>
        <el-table-column label="导航栏" width="70" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleNavStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.navStatus">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否显示" width="70" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleShowStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.showStatus">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" width="50" align="center">
          <template slot-scope="scope">{{scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="设置" width="140" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="openMappingForm(scope.$index, scope.row)">属性集合关联管理
            </el-button>
            <el-button
              size="mini"
              :disabled="scope.row.level | disableNextLevel"
              @click="handleShowNextLevel(scope.$index, scope.row)">查看下级
            </el-button>
            <el-button
              size="mini"
              @click="handleTransferProduct(scope.$index, scope.row)">转移商品
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleUpdate(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog title="关联属性集合"
               :visible.sync="mappingAttrCategoryFormDialogVisible"
               v-if="mappingAttrCategoryFormDialogVisible"
               :before-close="closeMappingForm"
               width="1200px"  append-to-body modal-append-to-body>
     <HaveMappingAttrCategoryList
       :categoryId="listSelectedCategoryId"
       v-on:updateSuccess="attrCategoryUpdateSuccessCallback"
     ></HaveMappingAttrCategoryList>

    </el-dialog>
  </div>

</template>

<script>
  import {fetchList,deleteProductCate,updateShowStatus,updateNavStatus} from '@/api/productCate'
  import HaveMappingAttrCategoryList from '../productAttr/components/HaveMappingAttrCategoryList'
  import {MappingAttrCategoryTypeItemList} from './type/CategoryType'
  export default {
    components: {
      HaveMappingAttrCategoryList
    },
    name: "productCateList",
    props: ['currentParentCategoryId'],
    data() {
      return {
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          pageNum: 1,
          pageSize: 5,
          keyWord: null,
          isMappingAttrCategory:null
        },
        parentId: 0,
        mappingAttrCategoryTypeItemList : MappingAttrCategoryTypeItemList,
        //映射关联Form模型
        mappingFormModel: {
          name:"",
          mappingAttrCategory:""
        },
        mappingAttrCategoryFormDialogVisible:false,
        //列表选中的分类ID
        listSelectedCategoryId:null,
        //已经映射的分类列表
        haveMappingAttrCategoryTypeItemList :[]

      }
    },
    created() {
      this.resetParentId();
      console.log(this.listQuery)
      this.getList();
    },
    watch: {
      $route(route) {
        this.resetParentId();
        this.getList();
      },
      'currentParentCategoryId': function(newVal) {
        console.log('Name changed to: ', newVal);
        this.resetParentId();
        this.getList();
      }
    },
    methods: {

      //查询
      search() {
        this.getList();
      },

      resetParentId(){
        this.listQuery.pageNum = 1;
        if (this.$route.query.parentId != null) {
          this.parentId = this.$route.query.parentId;
        } else {
          this.parentId = 0;
        }
        if(this.currentParentCategoryId && this.parentId ===  0){
          this.parentId =this.currentParentCategoryId;
        }
      },
      handleAddProductCate() {
        this.$router.push('/pms/addProductCate');
      },
      getList() {
        this.listLoading = true;
        fetchList(this.parentId, this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.records;
          this.total = response.data.total;
        });
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleNavStatusChange(index, row) {
        let data = new URLSearchParams();
        let ids=[];
        ids.push(row.id)
        data.append('ids',ids);
        data.append('navStatus',row.navStatus);
        updateNavStatus(data).then(response=>{
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000
          });
        });
      },
      handleShowStatusChange(index, row) {
        let data = new URLSearchParams();
        let ids=[];
        ids.push(row.id)
        data.append('ids',ids);
        data.append('showStatus',row.showStatus);
        updateShowStatus(data).then(response=>{
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000
          });
        });
      },
      handleShowNextLevel(index, row) {
        this.$router.push({path: '/pms/productCate', query: {parentId: row.id}})
      },
      handleTransferProduct(index, row) {
        console.log('handleAddProductCate');
      },

      /**
       * 打开映射属性集合Form
       * @param index
       * @param row
       */
      openMappingForm(index, row) {
          this.listSelectedCategoryId = row.id;
          this.mappingAttrCategoryFormDialogVisible=true;
      },

      /**
       * 关闭映射属性集合Form
       */
      closeMappingForm(done) {
        this.listSelectedCategoryId = null;
        this.mappingAttrCategoryFormDialogVisible=false;
      },

      /**
       * 映射属性集合更新成功回调
       */
      attrCategoryUpdateSuccessCallback(){
        this.closeMappingForm(null);
        this.getList();
      },


      handleUpdate(index, row) {
        this.$router.push({path:'/pms/updateProductCate',query:{id:row.id}});
      },
      handleDelete(index, row) {
        this.$confirm('是否要删除该品牌', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteProductCate(row.id).then(response => {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1000
            });
            this.getList();
          });
        });
      }
    },
    filters: {
      levelFilter(value) {
        if (value === 0) {
          return '一级';
        } else if (value === 1) {
          return '二级';
        }
      },
      disableNextLevel(value) {
        if (value === 0) {
          return false;
        } else {
          return true;
        }
      }
    }
  }
</script>

<style   >


.el-dialog__wrapper .el-dialog .el-dialog__body{
  height: 480px !important;
  padding: 5px 10px !important;
  overflow-y: scroll;
}
.el-button{
  margin-bottom: 1px !important;
  margin-top: 1px !important;
  margin-left: 0px !important;
}
</style>
